package com.qk.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.dto.role.RoleDTO;
import com.qk.entity.Role;
import com.qk.enums.ParamEnum;
import com.qk.exception.CommonException;
import com.qk.service.RoleService;
import com.qk.mapper.RoleMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author SweetHurt
 * @description 针对表【role(角色表)】的数据库操作Service实现
 * @createDate 2025-10-10 10:17:31
 */
@Service
@Slf4j
@SuppressWarnings("all")
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void updateRole(Role role) {
        boolean b = BeanUtil.hasNullField(role, "remark", "createTime, updateTime");
        if (b) {
            CommonException.throwException(ParamEnum.PARAM_NOT_NULL_ERROR);
        }
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.updateRole(role);
    }

    @Override
    public Role getById(Integer id) {
        Role role = roleMapper.selectById(id);
        return role;
    }

    @Override
    public void addRole(Role role) {
        boolean b = BeanUtil.hasNullField(role, "id", "label", "remark", "createTime", "updateTime");
        if (b) {
            CommonException.throwException(ParamEnum.PARAM_NOT_NULL_ERROR);
        }
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insertRole(role);
    }

    @Override
    public void deleteRole(Integer id) {
        roleMapper.deleteById(id);
    }

    @Override
    public PageResult<Role> getByPage(RoleDTO roleDTO) {
        PageInfo<Role> pageInfo = PageHelper
                .startPage(roleDTO.getPage(), roleDTO.getPageSize())
                .doSelectPageInfo(() -> roleMapper.selectByPage(roleDTO));
        return PageResult.<Role>builder()
                .total(Long.valueOf(pageInfo.getTotal()).intValue())
                .rows(pageInfo.getList())
                .build();
    }

    @Override
    public List<Role> getList() {
        List<Role> list = roleMapper.selectAll();
        return list;
    }
}




